package edu.hitsz.application;

import edu.hitsz.RankTable;
import edu.hitsz.aircraft.*;
import edu.hitsz.bullet.BaseBullet;
import edu.hitsz.basic.AbstractFlyingObject;
import edu.hitsz.prop.*;
import edu.hitsz.rank.RankDaoImpl;
import edu.hitsz.rank.RankInfo;
import edu.hitsz.shoot.*;
import edu.hitsz.shoot.ShootContext;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.concurrent.*;

import static edu.hitsz.aircraft.HeroAircraft._heroAircraft;

/**
 * 游戏主面板，游戏启动
 *
 * @author hitsz
 */
public abstract class Game extends JPanel {

    //线程判断标志
    public static int if_shoot = 0;
    public static int if_music = 1;

    private int backGroundTop = 0;

    private BufferedImage back_image;

    /**
     * Scheduled 线程池，用于任务调度
     */
    private final ScheduledExecutorService executorService;

    /**
     * 时间间隔(ms)，控制刷新频率
     */
    private int timeInterval = 40;

    private int num_boss = 1;
    private final HeroAircraft heroAircraft;
    private final List<MobEnemy> enemyAircrafts;
    private final List<Prop> props;
    private final List<BaseBullet> heroBullets;
    private final List<BaseBullet> enemyBullets;
    private BombEffect bombEffect = new BombEffect();

    /**
     * Enemy工厂
     */
    private final ElitePlusFactory elitePlusFactory = new ElitePlusFactory();
    private final BossFactory bossFactory = new BossFactory();

    /**
     * 子弹工厂
     */
    private int boss_bullets = 3;

    private final PropFactory blood_factory = new BloodFactory();
    private final PropFactory bullet_factory = new BulletFactory();
    private final PropFactory bullet_plus_factory = new BulletPlusFactory();
    private final PropFactory bomb_factory = new BombFactory();

    /**
     * 发射操作上下文
     */
    private ShootContext shootcontext = new ShootContext(new DirectShoot(_heroAircraft.getHeroAircraft(), -1));

    /**
     * 屏幕中出现的敌机最大数量
     */
    private int enemyMaxNumber = 5;

    /**
     * 当前得分
     */
    private int score = 0;
    /**
     * 当前时刻
     */
    private int time = 0;

    /**
     * 周期（ms)
     * 指示子弹的发射、敌机的产生频率
     */
    private int cycleDuration = 600;
    private int cycleTime = 0;

    /**
     * 游戏结束标志
     */
    private boolean gameOverFlag = false;

    public Game(int bg) {
        new MusicThread("src/bgm.wav", 1).start();
        heroAircraft = _heroAircraft.getHeroAircraft();
        enemyAircrafts = new LinkedList<>();
        heroBullets = new LinkedList<>();
        enemyBullets = new LinkedList<>();
        props = new LinkedList<>();

        if(bg == 0)
            back_image = ImageManager.BACKGROUND_IMAGE;
        else if (bg == 1) {
            back_image = ImageManager.BACKGROUND_IMAGE2;
        } else{
            back_image = ImageManager.BACKGROUND_IMAGE3;
        }

        /**
         * Scheduled 线程池，用于定时任务调度
         * 关于alibaba code guide：可命名的 ThreadFactory 一般需要第三方包
         * apache 第三方库： org.apache.commons.lang3.concurrent.BasicThreadFactory
         */
        this.executorService = new ScheduledThreadPoolExecutor(1,
                new BasicThreadFactory.Builder().namingPattern("game-action-%d").daemon(true).build());

        //启动英雄机鼠标监听
        new HeroController(this, heroAircraft);

    }

    /**
     * 游戏启动入口，执行游戏逻辑
     */
    public void action() {}

    //***********************
    //      Action 各部分
    //***********************

    abstract boolean timeCountAndNewCycleJudge();

    private void shootAction(){};

    private void bulletsMoveAction(){}

    private void aircraftsMoveAction() {}


    /**
     * 碰撞检测：
     * 1. 敌机攻击英雄
     * 2. 英雄攻击/撞击敌机
     * 3. 英雄获得补给
     */
    private void crashCheckAction() {}

    /**
     * 后处理：
     * 1. 删除无效的子弹
     * 2. 删除无效的敌机
     * <p>
     * 无效的原因可能是撞击或者飞出边界
     */
    private void postProcessAction() {}


    //***********************
    //      Paint 各部分
    //***********************

    /**
     * 重写paint方法
     * 通过重复调用paint方法，实现游戏动画
     *
     * @param  g
     */
    @Override
    public void paint(Graphics g) {}

    private void paintImageWithPositionRevised(Graphics g, List<? extends AbstractFlyingObject> objects) {}

    private void paintScoreAndLife(Graphics g) {}


}
